package com.ssextjs.component;

import java.io.IOException;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.ssextjs.engine.ConfiguraionImpl;
import com.ssextjs.engine.IdGenerator;

import freemarker.template.Template;
import freemarker.template.TemplateException;

public class Component {
	public Template tpl;
	public Map mainopt;
	public IdGenerator idgen;
	private int idc=1;
	
	public Component() {
		mainopt = new HashMap();
		idgen = new IdGenerator();
	}
	
	public void createConfig() {
		//System.err.println("CLASS: "+this.getClass().getName());
		Field[] fieldsParent = this.getClass().getFields();
		if (fieldsParent!=null) {
			for (int i=0; i<fieldsParent.length; i++) {
				Field fl = fieldsParent[i];
				if(!fl.getName().equals("mainopt") && !fl.getName().equals("tpl")) {
					try {
						//System.err.println("Find field: "+fl.getName());
						mainopt.put(fl.getName(), fl.get(this));
					} catch (IllegalArgumentException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IllegalAccessException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} 
				}
			}
		}

		mainopt.put("obj",this);
		mainopt.put("idgen", idgen);
	}
	
	public String getHTML() {
		createConfig();
		tplInit();
		return ProcessTemplate(tpl,mainopt);
	}
	
	public String toString() {
		createConfig();
		tplInit();
		return ProcessTemplate(tpl,mainopt);
	}
	
	public String ProcessTemplate(Template tp,Map opt) {
		String res="";
		StringWriter sw = new StringWriter();
		try {
			tp.process(opt, sw);
			res = sw.toString();
			sw.flush();
		} catch (TemplateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return res;
	}
	
	public void setMapOpt(Map opt) {
		Iterator it = opt.entrySet().iterator();
		while (it.hasNext()) {
			Entry ent = (Entry) it.next();
			String key = (String) ent.getKey();
			Object val = ent.getValue();
			try {
				Field fl = this.getClass().getField(key);
				fl.set(this, val);
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public String getId() {
		idc++;
		int id = this.hashCode();
		String ids="id_"+id+""+idc;
		return ids;
	}
	
	public Object getOpt(String optname) {
		try {
			Field fl = this.getClass().getField(optname);
			Object o = fl.get(this);
			if (o==null) {o="";}
			return o;
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";
	}
	
	public void tplInit() {
	}
}
